package com.cngphone.AnFetion;

import java.io.Serializable;
import java.util.ArrayList;

import android.content.Context;
import android.text.TextUtils;

import com.cngphone.AnFetion.Fetion.TcpSendAndReceive;
import com.cngphone.AnFetion.Fetion.SIPC.IMReceiver;
import com.cngphone.AnFetion.Fetion.SIPC.SerialManager;
import com.cngphone.AnFetion.tools.StrUtil;

public class FetionFriend implements Serializable, Comparable<FetionFriend> {
    private static final long serialVersionUID = -4889702395437832128L;

    private static final String BR = "\n";

    private String lastSpeaker = null;
    boolean isMe = false;

    public String Uri;
	private String FriendName;
    private String NickName;
    private String contactName;
    public String mobile_no;
    public String impresa;
    public long portrait;
    public int score = 0;
    public int type = 0;
    public int onlineNotify = 0;
    public int group = 0;

    public int notifyID;
    public String lastMessage;
    public boolean IsMobileNo;
    public boolean needUpdate = true;

    public boolean isNeedUpdate() {
		return needUpdate;
	}

	public void setNeedUpdate(boolean needUpdate) {
		this.needUpdate = needUpdate;
	}

	private int fetionID = 0;

    private ArrayList<FetionMessage> messageContent;
    private int onlineStatus;
    private String customStatus = null;
    
    private TcpSendAndReceive conn = null;

    public FetionFriend() {
        messageContent = new ArrayList<FetionMessage>();
    }

    public String getContactName() {
		return contactName;
	}

	public void setContactName(String contactName) {
		this.contactName = contactName;
	}

    public int getFetionID() {
        if (fetionID == 0) {
            FetionUri u = new FetionUri(Uri);
            fetionID = StrUtil.parseInt(u.getId(), 0);
        }
        return fetionID;
    }

    public String getFriendName() {
        return FriendName;
    }

    public void setFriendName(String friendName) {
        if (friendName != null && friendName.length() > 0) {
            FriendName = friendName;
        }
    }

    public String getNickName() {
        return NickName;
    }

    public void setNickName(String nickName) {
        if (nickName != null && nickName.length() > 0) {
            NickName = nickName;
        }
    }

    public int getOnlineStatusInt() {
        return onlineStatus;
    }

    public void setOnlineStatus(int onlineStatus) {
        setOnlineStatus(onlineStatus, null);
    }

    public void setOnlineStatus(int onlineStatus, String status) {
        this.onlineStatus = onlineStatus;
        this.customStatus = status;
    }

    public String getOndlineStatusStr() {
        if (customStatus != null) return "[" + customStatus + "]";
        return Settings.getStatusDesc(onlineStatus);
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();

        if (FriendName == null || FriendName.equals("")) sb.append(new FetionUri(this.Uri).id);
        else sb.append(FriendName);

        sb.append(" ").append(getOndlineStatusStr());

        if (impresa != null) sb.append(" " + impresa);

        return sb.toString();
    }

    public String toShortString() {
        StringBuffer sb = new StringBuffer();

        sb.append(getName());
        sb.append(" ").append(getOndlineStatusStr()).append(getType());
        return sb.toString();
    }

    public String addMessage(FetionMessage message) {
    	messageContent.add(message);
        return message.toString();
    }

    public ArrayList<FetionMessage> getMessage() {
        return messageContent;
    }
    
    public boolean isChatting () {
    	return messageContent.size() > 0;
    }

    @Override
    public int compareTo(FetionFriend another) {
        // System.out.println(this.FriendName + " vs " + another.FriendName);
        if (this.isMe) return -1;
        
        // BUGFIX: java.lang.NullPointerExeception
        // bangbang.song@gmail.com
        if (null == another) {
        	//TODO How can i do under this condition???
        	return -1;
        }
        if (another.isMe) return 1;
        if (this.isChatting() != another.isChatting()) {
        	if (this.isChatting()) return -1;
        	else return 1;
        }
        int ret = 0 - (this.getOnlineStatusInt() - another.getOnlineStatusInt());
        if (ret != 0) {
            if (this.getOnlineStatusInt() == Settings.STATUS_ONLINE) return -1;
            if (another.getOnlineStatusInt() == Settings.STATUS_ONLINE) return 1;
            return ret;
        }

        String s1 = this.getName();
        if (s1 == null) return 1;
        String s2 = another.getName();
        if (s2 == null) return -1;
        return s1.compareTo(s2);

    }
    
    public String getType() {
        return type == Settings.TYPE_REFUSE ? "(拒绝)" : type == Settings.TYPE_NOT_FRIEND ? "(等待同意)" :""; 
    }

    public String toFullString() {
        return "name = " + FriendName + "; nick = " + NickName + "; contactName = " + contactName 
        		+ "; uri = " + Uri + "; mobile = " + mobile_no
                + "; portrait = " + portrait + "; score = " + score + "; type = " + type
                + "; onlineNotify = " + onlineNotify + "; group = " + group + "; status = " + onlineStatus;
    }

    public String getName() {
        return (!TextUtils.isEmpty(contactName)) ? contactName : 
        	(FriendName != null ? FriendName : 
        		(NickName != null ? NickName : 
        			mobile_no != null ? mobile_no : new FetionUri(this.Uri).id));
    }
    
    public boolean isConnected() {
        return conn != null && !conn.isClosing();
    }
    
    public void close() {
        if (isConnected()) conn.close();
        conn = null;
    }
    
    public boolean connect(Context ctx, SerialManager serialManager, String ip, int port) {
        conn = TcpSendAndReceive.newInstance(ip, port);
        IMReceiver receiver = new IMReceiver(serialManager, conn, ctx);
        conn.addPackageReceivedQueue(receiver);
        if (conn.execute()) {
            return true;
        }
        close();
        return false;
    }
    
    public TcpSendAndReceive getConnection() {
        return conn;
    }

}
